package com.itboye.hutouben.activity.qianbao;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.presenter.UserPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.util.TimeCount;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

/**
 * 忘记原支付密码(1,获取短信验证码)
 */
public class WangJiZhiFuPassActivity extends BaseActivity implements Observer {
    ImageView close_icon;//返回图片
    TextView add_shap_title_tv, add_lift_tv;//标题
    TextView wangji_pass_phone;//手机号
    TextView wangji_pass_send;//发送验证码
    EditText wangji_pass_yzm;
    TextView pass_one_ok;//下一步
    UserPresenter userPresenter;
    String mobile;//手机号

    private TimeCount time;// 倒计时

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wang_ji_zhi_fu_pass);
        add_shap_title_tv.setVisibility(View.GONE);
        add_lift_tv.setText(R.string.zhaohui_zhifu_pass);
        userPresenter = new UserPresenter(this);
        mobile = (String) SPUtils.get(this, null, Const.MOBILE, "");

        if (!mobile.equals("")) {
            try {
                String hintphone = mobile.substring(0, 3) + "****"
                        + mobile.substring(7, 11);
                wangji_pass_phone.setText("请输入" + hintphone + "收到的短信校验码");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            wangji_pass_phone.setText("");
        }
        //返回
        IntentFilter intentFilterExit = new IntentFilter(
                "Wallet");
        registerReceiver(nichengBroadcastReceiver, intentFilterExit);
    }

    BroadcastReceiver nichengBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            finish();
        }

        ;
    };

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.wangji_pass_send://发送验证码
                userPresenter.sendVerificationCode("+86", mobile, 2 + "");
                time = new TimeCount(60000, 1000, wangji_pass_send);// 构造CountDownTimer对象
                time.start();
                break;
            case R.id.pass_one_ok:
                userPresenter.YanZhengverificationCode("+86", mobile, wangji_pass_yzm.getText().toString(), 2 + "");

                break;

            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == UserPresenter.send_code_success) {
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType() == UserPresenter.send_code_fail) {
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType() == UserPresenter.yanzheng_code_success) {
                ByAlert.alert(entity.getData());
                Intent intent = new Intent(this, WangJiZhiFuPassTwoActivity.class);
                startActivity(intent);
            }
            if (entity.getEventType() == UserPresenter.yanzheng_code_fail) {
                ByAlert.alert(entity.getData());
            }
        }
    }
}
